<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\Attr;
use think\Request;

class House
{
    public function show(Request $request){
        //可以根据区域，租金筛选以及根据租金排序
        //地理位置
        $station=$request->get('station');
        //房间大小
        $area=$request->get('area');
        //租金范围
        $price=$request->get('price');

        //租金排序顺序,1为正序，2为倒序
        $order=$request->get('order');
        $orders=[];
        $where=[];
        if($station){
            $where[]=['station','=',"$station"];
        }
        if($area){
            $where[]=['area','=',"$area"];
        }
        if($price){
            $where[]=['price','between',"$price"];
        }
       // dd($where);
        $res=\app\api\model\House::where($where)->paginate(2)->toArray();
        if($order){
            if($order==1){
                $res=\app\api\model\House::where($where)->paginate(2)->order('price','asc')->toArray();
            }else{
                $res=\app\api\model\House::where($where)->paginate(2)->order('price','desc')->toArray();
            }
        }
       $data=$res['data'];
       foreach ($data as $k=>$val){
          $ids= explode(',',$val['attr_ids']);
          $val['attr']=Attr::field('attr_name')->whereIN('id',$ids)->select()->toArray();
          unset($val['attr_ids']);
         $data[$k]=$val;
       }
      return successes('查询成功',$data);

    }
}
